<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="中文名称" prop="c_name">
              <el-input v-model="form.c_name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="英文名称" prop="e_name">
              <el-input v-model="form.e_name" oninput="value=value.replace(/[^\a-zA-Z]/g,'')" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数据类型" prop="data_type">
              <el-select v-model="form.data_type" placeholder="数据类型" clearable style="width: 100%">
                <el-option
                  v-for="(dict,index) in type"
                  :key="index"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="对应业务" prop="corresponding_business">
              <el-select v-model="form.corresponding_business" multiple placeholder="对应业务" clearable style="width: 100%">
                <el-option
                  v-for="(dict,index) in business"
                  :key="index"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { adddataAdd, adddataEdit } from '@/api/basics'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    },
    type: {
      type: Array
    },
    business: {
      type: Array
    }
  },
  data() {
    return {
      rules: {
        c_name: [
          { required: true, message: '请填写中文名称', trigger: 'blur' }
        ],
        e_name: [
          { required: true, message: '请填写英文名称', trigger: 'blur' }
        ],
        data_type: [
          { required: true, message: '请选择数据类型', trigger: 'change' }
        ],
        corresponding_business: [
          { required: true, message: '请选择对应业务', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      adddataAdd(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      adddataEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

